<?php
include_once "./libChart/libchart-1.1/libchart/libchart.php";
include_once('./db.conf');
// Connects to your Database 
$connection=mysqli_connect(DBHOST, DBUSER, DBPASS,DBDBNAME); 
if(isset($_COOKIE['CompanyCookie']))
{
$query = "call getAnswersForSurvey($_POST[survey])";
if($check=mysqli_query($connection,$query)){
	//get answers of survey
	$coun=0;
	if(mysqli_num_rows($check)==0)//check if the answers exist
		die('There Is No Answers For This Survey!!!');
	while($row=mysqli_fetch_assoc($check)){
		$answersFromDB[$coun]=$row['value'];
		$coun++;
	}
}
$i=-1;
$j=0;
//checking for the answers. if they are duplicated and the number of the answers
while($j<=$coun){
	for($j;$j<=$coun;$j++){
		$same=false;
		//check if this answer is the same with the previous ones
		for($countDown=$j-1; $countDown>=0;$countDown--){
			if($answersFromDB[$j]==$answersFromDB[$countDown]){
				$same=true;//we already have this answer
				break;
			}
		}
		if($same){//we already have this answer
			$j++;//initialize to look at next answer
			break;
		}
		else{//if we dont have this answer before
			$i++;
			$numberOfAnswers[$i]=1;
			$answerForChart[$i]=$answersFromDB[$j];
		}
		for($k=j+1;$k<=$coun ;$k++){
			if($answersFromDB[$j]==$answersFromDB[$k]){//we found the same answer
				$numberOfAnswers[$i]++;//so increase the number of the answer
			}
		}
	}
}
$chart = new PieChart(1000, 600);
	for($j=0;$j<$i;$j++){
		$answerForChart[$j]=$answerForChart[$j]."(".$numberOfAnswers[$j].")";
		$chart->addPoint(new Point($answerForChart[$j], $numberOfAnswers[$j]));
	}
$chart->setTitle("Results For Selected Survey");
	$chart->render("./chart.png");
mysqli_close($connection);
?> 
<html>
<head>
	<title>RESULTS FOR THE GIVEN SURVEY</title>
</head>
<body>
	<img alt="Vertical bars chart" src="./chart.png" style="border: 1px solid gray;"/>
	<form action="showSurveys.php">
	<input type="submit" name="back" value="<<<Go Back To Surveys">
</body>
</html>
<?php
}
else
	die('Session Expired Please Login...');
?>
